package com.aia.prm.web.ria.app.ui.config;

import java.util.List;

import org.apache.log4j.Logger;

import com.aia.prm.BeanFactory;
import com.aia.prm.web.ria.app.PrmApplication;
import com.cfh.persistence.dao.ConfigDAO;
import com.vaadin.data.Item;
import com.vaadin.data.Property;
import com.vaadin.data.Property.ValueChangeEvent;
import com.vaadin.terminal.Sizeable;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickListener;
import com.vaadin.ui.Form;
import com.vaadin.ui.HorizontalSplitPanel;
import com.vaadin.ui.Label;
import com.vaadin.ui.VerticalLayout;
import com.vaadin.ui.VerticalSplitPanel;
import com.vaadin.ui.Button.ClickEvent;

public class ConfigView extends VerticalLayout implements Property.ValueChangeListener {
	private static final Logger logger = Logger.getLogger(ConfigView.class);
	private PrmApplication app = null;
	VerticalSplitPanel vert = null;
	HorizontalSplitPanel horiz = null;
	VerticalLayout rightLayout = null;
	ConfigList configList = null;
	Form form = null;
	ConfigContainer configContainer = null;
	ConfigForm configForm = null;

	public ConfigView(PrmApplication aapp) {
		app = aapp;
		prepareData();
		buildMainLayout();
		load();
	}

	private void prepareData() {
		try {
			configContainer = new ConfigContainer();
			configForm = new ConfigForm(this);
		} catch (Exception e) {
			logger.equals(e);
			throw new RuntimeException(e);
		}
	}

	void load() {
		ConfigDAO configDAO = BeanFactory.get().getDefaultService().getConfigDAO();
		List list = configDAO.selectByExample(null);
		configContainer.removeAllItems();
		configContainer.addAll(list);
		//configList.setContainerDataSource(null);
		configList.setContainerDataSource(configContainer);
		//configList.select(null);

		//configForm.setItemDataSource(null);
		
		configList.setVisibleColumns(ConfigContainer.NATURAL_COL_ORDER);
		configList.setColumnHeaders(ConfigContainer.COL_HEADERS_SHOW);
	}

	private void buildMainLayout() {
		setSizeFull();
		vert = new VerticalSplitPanel();
		vert.setSizeFull();
		vert.setSplitPosition(60, Sizeable.UNITS_PIXELS);
		addComponent(vert);

		// add a label to the upper area
		vert.setFirstComponent(new Label("*该页面可以更改系统参数"));

		
		horiz = new HorizontalSplitPanel();
		horiz.setSplitPosition(66); // percent
		vert.setSecondComponent(horiz);

		// left component:
		configList = new ConfigList(app, this);
		horiz.setFirstComponent(configList);

		rightLayout = new VerticalLayout();
		rightLayout.setSizeUndefined();
		rightLayout.setWidth("100%");
		rightLayout.setMargin(true);
		rightLayout.addComponent(configForm);
		// right component:
		horiz.setSecondComponent(rightLayout);

	}

	public void valueChange(ValueChangeEvent event) {
		Item item = configList.getItem(configList.getValue());
		// if (item != configForm.getItemDataSource()) {
		configForm.setItemDataSource(null);
		configForm.setItemDataSource(item);
		// }
		// Property property = event.getProperty();
		// if (property == configList) {
		// Item item = configList.getItem(configList.getValue());
		// //if (item != configForm.getItemDataSource()) {
		// configForm.setItemDataSource(item);
		// //}
		// }
	}

}
